package com.ttc.mBackup.BackupAgent;

import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

public class MyServiceAgent extends Service {
	
	
	
	private static final String TAG = "MyService";
	private int hour;
	private int minute;
	private ArrayList<String> doW=null;
	private ArrayList<Integer> doM=null;
	private Timer timer = new Timer();
	private static long UPDATE_INTERVAL = 10000;
	private final IBinder mBinder = new MyBinder();
	private  ScheduleBackup se;
	/*Data receive
	 *1 : Daily
	 * 2 : Weekly
	 * 3 : Monthly
	 * */
	
	
	
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return mBinder;
	}
	
	
	private void pollForUpdates(int id) {
		
		switch (id) {
		case 1:
			timer.scheduleAtFixedRate(new TimerTask() {
				@Override
				public void run() {
					Date date = new Date();
					if(hour==date.getHours()&&minute==date.getMinutes())
					{
						
						UPDATE_INTERVAL=3600000;
							se.execute();
						
					}
					
					Log.i(getClass().getSimpleName(), "Daily Backup Check......");
					}
			}, 0, UPDATE_INTERVAL);
		
			break;

		case 2:
			
			timer.scheduleAtFixedRate(new TimerTask() {
				@Override
				public void run() {
					
					Date date = new Date();
					if(doW.contains(DateFormat.format("EEEE", new Date()).toString()))
					{
						
						if(date.getHours()==12)
						{
							UPDATE_INTERVAL=3600000;
							se.execute();
							
						}
					}
					
					Log.i(getClass().getSimpleName(), "Weekly Backup Check.....");
					}
			}, 0, UPDATE_INTERVAL);
		
			break;
		case 3 :
			timer.scheduleAtFixedRate(new TimerTask() {
				@Override
				public void run() {
					Date date = new Date();
					
					if(doM.contains(date.getDay()))
					{
						if(date.getHours()==12)
						{
							UPDATE_INTERVAL=3600000;
							se.execute();
							
						}
					}
					
					Log.i(getClass().getSimpleName(), "Monthly Backup Check...");
					}
			}, 0, UPDATE_INTERVAL);
			
			break;
		}
		Log.i(getClass().getSimpleName(), "Timer started.");

	}
	

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
		if (timer != null) {
			timer.cancel();
			if(se!=null)
			{
				se.cancel(true);
			}
			
		}
		Log.i(getClass().getSimpleName(), "Timer stopped.");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onStart");
		
		Bundle data = intent.getExtras();
		hour = data.getInt(BARScheduleType.KEY_HOURS);
		
		minute = data.getInt(BARScheduleType.KEY_MINUTE);
		
		doW = data.getStringArrayList(BARScheduleType.KEY_WEEKLY);
		
		se = new ScheduleBackup(MyServiceAgent.this);
		
		doM = data.getIntegerArrayList(BARScheduleType.KEY_MONTHLY);
		
		pollForUpdates(data.getInt(BARScheduleType.KEY_TYPE_SCHEDULE));
		
		
	}
	
	public class MyBinder extends Binder {
		MyServiceAgent getService() {
			return MyServiceAgent.this;
		}
	}
	
	
	
}
